package cn.tedu._03_vehicle.controller;

import cn.tedu._03_vehicle.base.response.JsonResult;
import cn.tedu._03_vehicle.base.response.StatusCode;
import cn.tedu._03_vehicle.mapper.UserMapper;
import cn.tedu._03_vehicle.pojo.dto.UserLonginParam;
import cn.tedu._03_vehicle.pojo.dto.UserRegParam;
import cn.tedu._03_vehicle.pojo.entity.User;
import cn.tedu._03_vehicle.pojo.vo.UserRegQuery;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;

@RestController
@RequestMapping("/v1/user")
@Slf4j
@Api(tags = "01.用户管理")

public class UserController {
    @Autowired
    private UserMapper userMapper;
    @ApiOperation("用户注册")
    @ApiOperationSupport(order = 10)
    @PostMapping("reg")
    // @Validated校验数据合法性
    public JsonResult reg(@RequestBody @Validated UserRegParam userRegParam){
        log.debug("userRegParam"+userRegParam);
        //先要验证数据的合法性
        //验证用户名是否重复1、要去查询是否有该用户的用户名  2、有则失败，没有则插入数据返回成功
        String username = userRegParam.getUsername();
        UserRegQuery userRegQuery = userMapper.selectByUsername(username);
        //1、要去查询是否有该用户的用户名  2、有则失败，没有则插入数据返回成功
        //log.debug(userRegQuery.toString());
        if (userRegQuery!=null){//被占用了
            return new JsonResult(StatusCode.USERNAME_EXISTS);
        }
        User user = new User();
        BeanUtils.copyProperties(userRegParam,user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        int i = userMapper.insert(user);
        log.debug("user = " + user);
        return JsonResult.ok();
    }
    //登录功能
    @PostMapping("login")
    @ApiOperation("登录功能")
    @ApiOperationSupport(order = 20)
    //HttpSession 保持回话的接口
    public JsonResult login(@RequestBody UserLonginParam userLonginParam, @ApiIgnore HttpSession session){
        //拿到用户传过来的数据并打印出来
        log.debug("userLoginParam="+ userLonginParam);

        //拿到数据后要去数据库查询做比较，如果不对，就失败，如果对，就响应登录成功
        String username = userLonginParam.getUsername();
        String password = userLonginParam.getPassword();
        UserRegQuery userRegQuery = userMapper.selectByUsernameAndPassword(username, password);
        //做判断
        if (userRegQuery !=null){
            //将用户的标识存入到服务器端（暂时存储到内存中）
            session.setAttribute("user",userRegQuery);
            return JsonResult.ok(userRegQuery);
        }
        return new JsonResult(StatusCode.USER_PASSWORD_ERROR);
    }
    //获取当前用户功能
    @GetMapping("currentUser")
    @ApiOperation("获取当前用户")
    @ApiOperationSupport(order = 30)
    public JsonResult currentUser(@ApiIgnore HttpSession session){
        //从数据库中拿到唯一标识符，然后存储到UserRegQuery类中的user中
        UserRegQuery user = (UserRegQuery) session.getAttribute("user");
        /*这里拿到的数据可能有两种
        * 1、 new JsonResult.ok(null)这个相当于是未登录状态
        * 1、 new JsonResult.ok(user)这个相当于是登录状态
        * */
        return JsonResult.ok(user);
    }
    //退出登录功能
    /**
     * 怎么验证： 1 先有登录
     * 2然后查看当前用户
     * 3 退出登录
     * 4再查看当前用户
     */
    @GetMapping("logout")
    @ApiOperation("退出登录")
    @ApiOperationSupport(order = 40)
    public void logout(@ApiIgnore HttpSession session){
        session.removeAttribute("user");
    }
}
